Imports pixolut.BizBlox
Imports NUnit.Framework

<TestFixture()> Public Class DOTests
    Public Sub New()
        MyBase.New()
    End Sub

    <TestFixtureSetUp()> Public Sub GlobalInit()
        'do nothing...
    End Sub

    <TestFixtureTearDown()> Public Sub GlobalDestruct()
        'do nothing...
    End Sub

    <SetUp()> Public Sub LocalInit()
        'do nothing...
    End Sub

    <TearDown()> Public Sub LocalDestruct()
        Setup.ClearDataSet()
    End Sub

    <Test()> Public Sub testCreateNew()
        Setup.CreateTestStudent()
    End Sub

    <Test(), ExpectedException(GetType(ArgumentOutOfRangeException))> Public Sub testBoundsCheck()
        Dim obj As Food = Setup.CreateTestFood()
        Dim bigName As String = ""
        For i As Integer = 0 To 60
            bigName = bigName & Dictionary.RandomEntry()
        Next
        Console.WriteLine("Overflow: " & bigName)
        obj.Name = bigName
        obj.Save()
    End Sub

    <Test()> Public Sub testGetBy()
        Dim fObj As Food = Setup.CreateTestFood
        Dim sObj As Student = Setup.CreateTestStudent
        Dim order As New LunchOrder
        order.dt_OrderDate = Now
        order.FoodID = fObj.FoodID
        order.StudentID = sObj.StudentID
        order.Quantity = 1
        order.Save()

        Dim foodName As String = order.GetByFoodID().Name
        Assert.AreEqual(Setup.TestObjectFoodName, foodName)
    End Sub

    <Test()> Public Sub testNewRetrieveExisting()
        Dim obj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = obj.StudentID
        Dim rObj As New Student(uid)
        rObj.Load()
        Assert.AreEqual(rObj.StudentID, obj.StudentID)
        Assert.AreEqual(rObj.Name, obj.Name)
        Assert.AreEqual(rObj.Email, obj.Email)
        Assert.AreEqual(rObj.Age, obj.Age)
    End Sub

    <Test(), ExpectedException(GetType(DOException))> Public Sub testDelete()
        Dim Obj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = Obj.StudentID
        Dim rObj As New Student(uid)
        rObj.Delete()

        Dim dobj As New Student(uid)
        dobj.Load()
    End Sub


    <Test()> Public Sub testNullInt()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim fObj As Food = Setup.CreateTestFood()

        Dim lObj As New LunchOrder
        lObj.dt_OrderDate = Now
        lObj.StudentID = sObj.StudentID
        lObj.Quantity = Nothing
        lObj.FoodID = fObj.FoodID
        lObj.Save()

        Dim xObj As New LunchOrder(lObj.LunchOrderID)
        xObj.Load()
        Assert.AreEqual(0, xObj.Quantity)
    End Sub

    <Test()> Public Sub testNullDouble()
        Dim fObj As New Food
        fObj.Name = "test"
        fObj.Price = Nothing
        fObj.Save()

        Dim xObj As New Food(fObj.FoodID)
        xObj.Load()
        Assert.AreEqual(0, xObj.Price)
    End Sub


    <Test()> Public Sub testNullString()
        Dim fObj As New Food
        fObj.Name = Nothing
        fObj.Price = 100
        fObj.Save()

        Dim xObj As New Food(fObj.FoodID)
        xObj.Load()
        Assert.AreEqual("", xObj.Name)
    End Sub

    <Test()> Public Sub testNullDate()
        Dim sObj As Student = Setup.CreateTestStudent()
            Dim fObj As Food = Setup.CreateTestFood()

        Dim lObj As New LunchOrder
        lObj.dt_OrderDate = Nothing
        lObj.StudentID = sObj.StudentID
        lObj.Quantity = 100
        lObj.FoodID = fObj.FoodID
        lObj.Save()
        Assert.IsTrue(DataObject.IsNull(lObj.dt_OrderDate))
    End Sub

    <Test()> Public Sub testNullDatePersist()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim fObj As Food = Setup.CreateTestFood()

        Dim lObj As New LunchOrder
        lObj.dt_OrderDate = Nothing
        lObj.StudentID = sObj.StudentID
        lObj.Quantity = 100
        lObj.FoodID = fObj.FoodID
        lObj.Save()

        Dim xObj As New LunchOrder(lObj.LunchOrderID)
        xObj.Load()
        Assert.IsTrue(DataObject.IsNull(xObj.dt_OrderDate))
    End Sub

    <Test()> Public Sub testNullDateRetrieve()
        Dim studentId As Guid = Guid.NewGuid()
        Dim lunchID As Guid = Guid.NewGuid()
        Dim foodID As Guid = Guid.NewGuid()
        Dim dObj As New DBAccess
        dObj.RunSQL("INSERT INTO [Food](FoodID, Name, Price)VALUES('" & foodID.ToString() & "','Crud','1.23')", CommandType.Text)
        dObj.RunSQL("INSERT INTO [Student](StudentID, Name, Email, Age)VALUES('" & studentId.ToString() & "','Joe Test','joe@pixolut.com', '19')", CommandType.Text)
        dObj.RunSQL("INSERT INTO [LunchOrder](LunchOrderID, StudentID, FoodID, dt_OrderDate, Quantity) VALUES('" & lunchID.ToString() & "','" & studentId.ToString() & "', '" & foodID.ToString() & "', NULL , '1')", CommandType.Text)
        Dim lObj As New LunchOrder(lunchID)
        lObj.Load()
        Console.WriteLine(lObj.dt_OrderDate.ToString())
        Assert.AreEqual(foodID.ToString(), lObj.FoodID.ToString())
        Console.WriteLine(lObj.FoodID.ToString())
    End Sub

    <Test()> Public Sub testNullGuid()
        Try
            Dim sObj As Student = Setup.CreateTestStudent()
            Dim fObj As Food = Setup.CreateTestFood()
            Dim lObj As New LunchOrder
            lObj.dt_OrderDate = Now
            lObj.StudentID = Nothing
            lObj.Quantity = 1
            lObj.FoodID = fObj.FoodID
            lObj.Save()
        Catch ex As DOException
            Console.WriteLine("Expected exception output:" & ex.ToString())
            'validate NULL is actually being inserted in to the table by trying to insert in a NOT NULL column.
            Assert.IsTrue(ex.ToString().IndexOf("Cannot insert the value NULL into column 'StudentID'") > -1)
            Return
        End Try
        Assert.Fail()
    End Sub

    <Test()> Public Sub testNullGuidRetrieve()
        Dim studentId As Guid = Guid.NewGuid()
        Dim lunchID As Guid = Guid.NewGuid()
        Dim dObj As New DBAccess
        dObj.RunSQL("INSERT INTO [Student](StudentID, Name, Email, Age)VALUES('" & studentId.ToString() & "','Joe Test','joe@pixolut.com', '19')", CommandType.Text)
        dObj.RunSQL("INSERT INTO [LunchOrder](LunchOrderID, StudentID, FoodID, dt_OrderDate, Quantity) VALUES('" & lunchID.ToString() & "','" & studentId.ToString() & "', null, '" & Now.AddDays(Rnd() * 5).ToString("u") & "', '1')", CommandType.Text)
        Dim lObj As New LunchOrder(lunchID)
        lObj.Load()
        Console.WriteLine(lObj.FoodID.ToString())
    End Sub

End Class
